package com.lgj.springboot_lgj.web.controller;

import com.github.pagehelper.PageInfo;
import com.lgj.springboot_lgj.bean.basic.Category;
import com.lgj.springboot_lgj.service.ICategoryService;
import com.lgj.springboot_lgj.utils.Result;
import com.lgj.springboot_lgj.utils.ResultUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.lang.reflect.Array;
import java.util.List;

/**
 * @Author qjl
 * @Date 2022/6/19
 */
@Api(tags = "资讯栏目模块")
@RestController
@RequestMapping("/auth/category")
public class CategoryController {
    @Autowired
    private ICategoryService service;
    @Autowired
    private Category category;


    @ApiOperation("查询所有栏目信息")
    @GetMapping("/find/all")
    public Result findAllCategory(){
        List<Category> list = service.findAll();
        return ResultUtil.success(list);
    }

    @ApiOperation("保存栏目信息")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "categoryName",value = "分类名称",required = true),
            @ApiImplicitParam(name = "categoryDescription",value = "栏目描述",required = true),
            @ApiImplicitParam(name = "categoryOrder",value = "栏目序号",required = true),
            @ApiImplicitParam(name = "categoryParentId",value = "栏目所属的父栏目")
    })
    @PostMapping("/save")
    public Result saveCategory(String categoryName,String categoryDescription,Integer categoryOrder,Integer categoryParentId){
        category.setCategoryName(categoryName);
        category.setCategoryDescription(categoryDescription);
        category.setCategoryOrder(categoryOrder);
        category.setCategoryParentId(categoryParentId);
        service.saveCategory(category);
        return ResultUtil.success("保存栏目成功");
    }

    @ApiOperation("更新栏目信息")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "categoryId",value = "编号",required = true),
            @ApiImplicitParam(name = "categoryName",value = "分类名称",required = true),
            @ApiImplicitParam(name = "categoryDescription",value = "栏目描述",required = true),
            @ApiImplicitParam(name = "categoryOrder",value = "栏目序号",required = true),
            @ApiImplicitParam(name = "categoryParentId",value = "栏目所属的父栏目id")
    })
    @PutMapping("/update")
    public Result updateCategory(Integer categoryId,String categoryName,String categoryDescription,Integer categoryOrder,Integer categoryParentId){
        category.setCategoryId(categoryId);
        category.setCategoryName(categoryName);
        category.setCategoryDescription(categoryDescription);
        category.setCategoryOrder(categoryOrder);
        category.setCategoryParentId(categoryParentId);
        service.updateCategory(category);
        return ResultUtil.success("更新栏目成功");
    }

    @ApiOperation("分页查询栏目信息")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "pageNum",value = "页数",required = true),
            @ApiImplicitParam(name = "pageSize",value = "每页条数",required = true)
    })
    @GetMapping("/page/queryAll")
    public Result pageAll(int pageNum, int pageSize){
        PageInfo<Category> data = service.pageQueryAll(pageNum,pageSize);
        return ResultUtil.success(data);
    }

    @ApiOperation("通过栏目编号删除栏目")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id",value = "栏目id",dataType = "int",required = true)
    })
    @DeleteMapping("/deleteById")
    public Result deleteById2(Integer id){
        service.deleteById(id);
        return ResultUtil.success("删除成功");
    }

    @ApiOperation("通过id批量删除栏目")
    @DeleteMapping("/batchDelete")
    public Result batchDelete2(int[] ids){
        String meg = service.batchDelete(ids);
        if (meg.equals("删除失败，请输入正确的id")){
            return ResultUtil.category_error();
        }
        return ResultUtil.success("删除成功");
    }

}
